﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Reflection;

namespace Yz.Core.Extensions
{
    public static class EnumExtension
    {
        /// <summary>
        ///     返回列举项的描述信息。
        /// </summary>
        /// <param name="value">要获取描述信息的列举项。</param>
        /// <returns>列举想的描述信息。</returns>
        public static string GetDescriptions(this Enum value, bool isTop = false)
        {
            var enumType = value.GetType();
            DescriptionAttribute attr = null;
            if (isTop)
            {
                attr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute));
            }
            else
            {
                // 获取列举常数名称。
                var name = Enum.GetName(enumType, value);

                if (name != null)
                {
                    // 获取列举字段。

                    var fieldInfo = enumType.GetField(name);
                    if (fieldInfo != null)
                        attr =
                            Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as
                                DescriptionAttribute;
                }
            }

            if (attr != null && !string.IsNullOrEmpty(attr.Description))
                return attr.Description;
            return string.Empty;
        }

        public static string GetDescription(this Enum em)
        {
            Type type = em.GetType();
            FieldInfo fd = type.GetField(em.ToString());
            if (fd == null)
                return string.Empty;
            object[] attrs = fd.GetCustomAttributes(typeof(DescriptionAttribute), false);
            string name = string.Empty;
            foreach (DescriptionAttribute attr in attrs)
            {
                name = attr.Description;
            }
            return name;
        }

        public static List<dynamic> GetAllItems(this Type enumType)
        {
            var list = new List<dynamic>();
            // get enum fileds
            var fields = enumType.GetFields();
            foreach (var field in fields)
            {
                if (!field.FieldType.IsEnum) continue;
                // get enum value
                var value = (int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null);
                var name = field.Name;
                var description = string.Empty;
                var array = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (array.Length > 0)
                    description = ((DescriptionAttribute)array[0]).Description;
                else
                    description = ""; //none description,set empty
                //add to list
                dynamic obj = new ExpandoObject();
                obj.Value = value;
                obj.Name = name;
                obj.Description = description;
                list.Add(obj);
            }

            return list;
        }


        /// <summary>
        ///     根据枚举描述获取枚举的值
        /// </summary>
        /// <param name="description">枚举描述</param>
        /// <returns></returns>
        public static int GetEnumByDescription(this Type enumType, string description)
        {
            var enumlist = enumType.GetAllItems();
            var value = enumlist.Where(e => e.Description == description).Select(e => e.Value).FirstOrDefault();
            if (value == null) return -1;
            return value;
        }
    }
}
